package ee.logsimulator.ui;

import ee.logsimulator.Logsimulator;
import ee.logsimulator.Model;
import ee.logsimulator.parser.HtmlParser;
import ee.logsimulator.parser.data.Boss;
import ee.logsimulator.parser.data.Raid;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;
import java.util.Map.Entry;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import org.htmlparser.util.ParserException;

public class BossPage extends MessageDialog {

  public BossPage(final Logsimulator frame, final Model model, String url, final Raid raid) throws ParserException, IOException {
    super(frame, "Select boss", true);

    HtmlParser parser = new HtmlParser();
    parser.parseRaidPage(url, raid);

    JPanel panel = new JPanel();
    panel.setBackground(Color.BLACK);
    panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));

    for (final Entry<String, List<Boss>> bosses : raid.getBosses().entrySet()) {
      JPanel p = new JPanel();
      p.setBackground(Color.BLACK);
      p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
      for (final Boss boss : bosses.getValue()) {
        JButton b = new JButton(boss.name);
        b.setFocusPainted(false);
        b.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            try {
              final MessageDialog d = new MessageDialog(BossPage.this, "downloading", true);
              d.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
              d.setLocationRelativeTo(BossPage.this);
              final JProgressBar progressBar = new JProgressBar(0, 100);
              d.add(progressBar);
              progressBar.setValue(0);
              progressBar.setStringPainted(false);
              progressBar.setIndeterminate(true);
              d.pack();
              Thread worker = new Thread(new Runnable() {

                @Override
                public void run() {
                  frame.selectRaid(boss, raid);
                  d.dispose();
                  BossPage.this.dispose();
                }
              });
              worker.start();
              d.setVisible(true);
            } catch (Exception e1) {
              frame.parseException(e1);
            }
          }
        });
        b.setBackground(Color.BLACK);
        b.setForeground(boss.isWipe() ? Color.RED : Color.GREEN);
        b.setAlignmentX(Component.LEFT_ALIGNMENT);
        p.add(b);
      }

      panel.add(p);
    }

    ScrollPane scroll = new ScrollPane();
    scroll.add(panel);
    add(scroll, BorderLayout.CENTER);

    pack();
    Dimension size = panel.getSize();
    if (size.width > frame.getWidth() - 100) {
      size.width = frame.getWidth() - 100;
      size.height = size.height + 25;
    }
    if (size.height > frame.getHeight() - 50) {
      size.height = frame.getHeight() - 50;
      size.width = size.width + 25;
    }
    scroll.setSize(size);
    pack();
    setResizable(false);
    setLocationRelativeTo(frame);
    setVisible(true);
  }

  private static final long serialVersionUID = 1L;

}
